﻿using System;
using System.IO;
using EvoGame.Core.Components;
using EvoGame.Core.Interfaces;

namespace EvoGame.Core.Files
{
	public static partial class FileHandler
	{
		public class Log : File, ISaveable
		{
			protected override FileStream fileStream
			{
				get
				{
					try
					{
						return this.file.Open (FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
					}
					catch (DirectoryNotFoundException)
					{
						Directory.CreateDirectory (this.file.DirectoryName);
						return this.file.Open (FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
					}

				}
			}

			private TextWriter txtWrite;

			public Log ()
				: base (String.Format ("{0}\\EvoGame\\{1}.log", Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), EvoApp.Name))
			{
				this.txtWrite = new StreamWriter (fileStream);
			}
			public Log (string path)
				: base (path)
			{
				this.txtWrite = new StreamWriter (fileStream);
			}

			public void Write (string input)
			{
				this.txtWrite.Write (input);
			}

			public void Save ()
			{
				this.txtWrite.Flush ();
			}

			public override void Close ()
			{
				this.txtWrite.Close ();
			}
		}
	}
}